/**
 * @Author: LTT 17386012013@163.com
 * @Date: 2024-12-12 08:57:25
 * @Description:
 * @LastEditTime: 2024-12-12 10:20:03
 * @LastEditors: LTT 17386012013@163.com
 */
import { Vec3, Quat, Node } from 'cc';

export class Rotation {
	private static startP: Vec3 = new Vec3();
	private static tempV3: Vec3 = new Vec3();
	private static tempQ4: Quat = new Quat();

	static angle2Rad(angle: number): number {
		return (angle * Math.PI) / 180;
	}

	public static rotationAroundNode(
		selfNode: Node,
		targetNode: Node,
		axis: Vec3,
		angle: number
	) {
		Rotation.startP.set(selfNode.worldPosition.clone());
		let rad = Rotation.angle2Rad(angle);
		// 计算四元数

		Quat.fromAxisAngle(Rotation.tempQ4, axis, rad);
		// // 获取相机相对物体的向量
		Vec3.subtract(Rotation.tempV3, Rotation.startP, targetNode.position);
		// 根据四元数旋转向量
		Vec3.transformQuat(Rotation.tempV3, Rotation.tempV3, Rotation.tempQ4);
		selfNode.setPosition(Rotation.tempV3.add(targetNode.position));
	}
}
